package com.app.piemoment.entity;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.app.piemoment.PieMomentApp;

/**
 * Created by IntelliJ IDEA. User: yinchong Date: 2010-7-20 Time: 15:33:13 To
 * change this template use File | Settings | File Templates.
 */
public class CitySelectDialog implements OnItemClickListener,
		View.OnClickListener {
	public static int TYPE_PROVINCE = 0;
	public static int TYPE_CITY = 1;
	public static int TYPE_TOWN = 2;
	public static String provinceString = "选择省份";
	public static String cityString = "选择地市";
	public static String townString = "选择区县";

	public static String SELECT_ALL = "  选择全国  ";
	public static String SELECT_PROVINCE = "  选择全省  ";
	public static String SELECT_CIYT = "  选择全市  ";

	private Context context;
	private PieMomentApp tApplication = null;
	private GridDialog normalDialog;
	private City city = new City();

	private CityListener clistener;
	private int whichOne;
	private int TYPE;

	public int getTYPE() {
		return TYPE;
	}

	public void setTYPE(int tYPE) {
		TYPE = tYPE;
	}

	public CitySelectDialog(Context context, int whichOne, City lastcity,
			CityListener clistener) {
		this.context = context;
		this.clistener = clistener;
		this.whichOne = whichOne;
		tApplication = (PieMomentApp) context.getApplicationContext();
		normalDialog = new GridDialog(context) {
			@Override
			public boolean onKeyDown(int keyCode, KeyEvent event) {
				super.onKeyDown(keyCode, event);
				if (keyCode == KeyEvent.KEYCODE_BACK) {
					setNowCity();
				}
				return false;
			}
		};

		setTYPE(TYPE_PROVINCE);
		normalDialog.setOnItemListener(this);
		normalDialog.setButtonOnClick(this);
		normalDialog.setLastCity(lastcity);
		normalDialog.refreshData(getList(TYPE_PROVINCE, ""), TYPE_PROVINCE,
				"全国");
		normalDialog.show();
	}

	public List<String> getList(int type, String name) {
		List<String> tList = new ArrayList<String>();
		if (type == TYPE_PROVINCE) {
			tList = tApplication.getProvincesList();
		} else if (type == TYPE_CITY) {
			tList = tApplication.getCitiesMap().get(name);
		} else if (type == TYPE_TOWN) {
			tList = tApplication.getDistrictsMap().get(name);
		}
		return tList;
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		GridView gv = (GridView) arg0;
		TextView tView = (TextView) arg1;
		List<String> lStrings = new ArrayList<String>();
		String cityString = tView.getText().toString();
		if (gv.getTag().equals(TYPE_PROVINCE)) {
			city.setProvince(cityString);
			lStrings = getList(TYPE_CITY, city.getProvince());
			if (lStrings != null && lStrings.size() > 0) {
				normalDialog.refreshData(lStrings, TYPE_CITY, cityString);
				setTYPE(TYPE_CITY);
			} else {
				clistener.onCityResult(city, whichOne);
				normalDialog.dismiss();
			}
		} else if (gv.getTag().equals(TYPE_CITY)) {
			city.setCity(cityString);
			lStrings = getList(TYPE_TOWN, city.getCity());
			if (lStrings != null && lStrings.size() > 0) {
				normalDialog.overCityString = cityString;
				normalDialog.refreshData(lStrings, TYPE_TOWN, cityString);
				setTYPE(TYPE_TOWN);
			} else {
				clistener.onCityResult(city, whichOne);
				normalDialog.dismiss();
			}
		} else if (gv.getTag().equals(TYPE_TOWN)) {
			city.setTown(cityString);
			clistener.onCityResult(city, whichOne);
			normalDialog.dismiss();
		}
	}

	public void setNowCity() {

		if (getTYPE() == TYPE_PROVINCE) {
			normalDialog.dismiss();
		} else if (getTYPE() == TYPE_CITY) {
			List<String> temp = new ArrayList<String>();
			temp.addAll(getList(TYPE_PROVINCE, ""));
			normalDialog.refreshData(temp, TYPE_PROVINCE, "全国");
			setTYPE(TYPE_PROVINCE);
		} else if (getTYPE() == TYPE_TOWN) {
			List<String> temp = new ArrayList<String>();
			temp.addAll(getList(TYPE_CITY, city.getProvince()));
			normalDialog.refreshData(temp, TYPE_CITY, city.getProvince());
			setTYPE(TYPE_CITY);
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (normalDialog.button.equals(v)) {
			if (normalDialog != null) {
				normalDialog.dismiss();
			}
		} else if (normalDialog.button2.equals(v)) {
			setNowCity();
		} else if (normalDialog.selectAllButton.equals(v)) {
			City select_allCity = new City();
			select_allCity = city;
			if (getTYPE() == TYPE_PROVINCE) {
				select_allCity.setProvince("全国");
				select_allCity.setCity("");
				select_allCity.setTown("");
				clistener.onCityResult(select_allCity, whichOne);
			} else if (getTYPE() == TYPE_CITY) {
				select_allCity.setCity("");
				select_allCity.setTown("");
				clistener.onCityResult(select_allCity, whichOne);
			} else if (getTYPE() == TYPE_TOWN) {
				select_allCity.setTown("");
				clistener.onCityResult(select_allCity, whichOne);
			}
			normalDialog.dismiss();
		}
	}
}
